package com.platform.api;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.LoginUser;
import com.platform.entity.CollectVo;
import com.platform.entity.PostsVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiCollectService;
import com.platform.service.ApiPostsService;
import com.platform.util.ApiBaseAction;
import com.platform.util.ApiPageUtils;
import com.platform.utils.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: @author Harmon <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@Api(tags = "用户收藏")
@RestController
@RequestMapping("/api/collect")
public class ApiCollectController extends ApiBaseAction {
    @Autowired
    private ApiCollectService collectService;
    @Autowired
    private ApiPostsService postsService;

    /**
     * 分页获取用户收藏列表
     *
     * @param loginUser
     * @param typeId    0-商品收藏；1-暖课
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "获取用户收藏")
    @RequestMapping("list")
    public Object list(@LoginUser UserVo loginUser,
                       @RequestParam Integer typeId,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "size", defaultValue = "10") Integer size) {

        Map<String, Object> params = new HashMap<>();
        params.put("user_id", loginUser.getUserId());
        params.put("type_id", typeId);
        params.put("page", page);
        params.put("limit", size);
        params.put("sidx", "id");
        params.put("order", "asc");

        Query query = new Query(params);

        List<CollectVo> collectEntities;

        if (typeId == 0) {
            collectEntities = collectService.queryList(params);
        } else if (typeId == 1) {
            collectEntities = collectService.queryList(params);
        } else {
            return toResponsFail("不存在该收藏类型");
        }

        int total = collectService.queryTotal(params);
        ApiPageUtils pageUtil = new ApiPageUtils(collectEntities, total, query.getLimit(), query.getPage());

        return toResponsSuccess(pageUtil);
    }

    /**
     * 添加或取消收藏
     *
     * @param loginUser
     * @param typeId    收藏类型：0-商品；1-暖课 ；2-社区故事
     * @param valueId
     * @return
     */
    @Transactional
    @RequestMapping("addordelete")
    public Object addordelete(@LoginUser UserVo loginUser, @RequestParam Integer typeId, @RequestParam Integer valueId) {

        Map<String, Object> param = new HashMap<>();
        param.put("user_id", loginUser.getUserId());
        param.put("type_id", typeId);
        param.put("value_id", valueId);

        List<CollectVo> collectEntities = collectService.queryList(param);

        //collectResult 收藏结果
        Integer collectRes;
        String handleType = "add";
        if (null == collectEntities || collectEntities.size() < 1) {
            CollectVo collectEntity = new CollectVo();
            collectEntity.setAdd_time(System.currentTimeMillis() / 1000);
            collectEntity.setType_id(typeId);
            collectEntity.setValue_id(valueId);
            collectEntity.setIs_attention(0);
            collectEntity.setUser_id(loginUser.getUserId());
            //添加收藏
            collectRes = collectService.save(collectEntity);

        } else {
            //取消收藏
            collectRes = collectService.delete(collectEntities.get(0).getId());
            handleType = "delete";
        }

        if (collectRes > 0) {
            Map<String, Object> data = new HashMap<>();
            data.put("type", handleType);
            //todo 业务处理
            if (typeId == 2) {//2-社区故事
                PostsVo postsVo = postsService.queryObject(valueId);
                if (postsVo != null) {
                    if (handleType.equals("add")) {
                        postsVo.setCollectCount(postsVo.getCollectCount() + 1);
                    } else {
                        if (postsVo.getCollectCount() > 0) {
                            postsVo.setCollectCount(postsVo.getCollectCount() - 1);
                        }
                    }
                    postsService.update(postsVo);
                }
            }
            return toResponsSuccess(data);
        }

        return toResponsFail("操作失败");
    }

    /**
     * 快速删除对应类型的所有收藏
     *
     * @param loginUser
     * @param typeId
     * @return
     */
    @RequestMapping("deleteAll")
    public Object deleteAll(@LoginUser UserVo loginUser, @RequestParam Integer typeId) {

        Map<String, Object> param = new HashMap<>();
        param.put("user_id", loginUser.getUserId());
        param.put("type_id", typeId);

        List<CollectVo> collectEntities = collectService.queryList(param);
        if (collectEntities.size() > 0) {
            for (CollectVo collectVo : collectEntities) {
                collectService.delete(collectVo.getId());
            }
        }

        return toResponsSuccess("");
    }


}